import Vue from 'vue'
import VueRouter from 'vue-router'
import Link from '@/apis/api.js'


Vue.use(VueRouter)

const routes = [

  {//主页
    path: '/home',
    name: 'dome',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue')
  },
  {//动态
    path: '/dongTai',
    name: 'dongTai',
    component: () => import('../views/dongTai.vue'),
    children: [
      {//动态-关注
        path: 'dongTaiGuanZhu',
        name: 'dongTaiGuanZhu',
        component: () => import('../views/dongTai_guanZhu.vue'),
      },
      {//动态-发现
        path: 'dongTaiFind',
        name: 'dongTaiFind',
        component: () => import('../views/dongTai_find.vue')
      }
    ]
  },

  {//消息
    path: '/news',
    name: 'news',
    component: () => import('../views/news.vue'),
    beforeEnter: (to, from, next) => {
      console.log(to);
      console.log(from);
        //拿到登录状态的token
      let token = window.localStorage.getItem('token')||'';
      console.log(token);
      //把token发送到后台解密比对
      Link('/api/MiYoShe/home/userlogin','get',{token}).then((ok) =>{
        console.log(ok);
        if(ok.data.login == 1){
          next();
        }else{
          alert('您还未登录，请先登录');
          next('/login');
        }
      })
    }
  },
  {//我的
    path: '/wode',
    name: 'wode',
    component: () => import('../views/wode.vue')
  },
  {//我的-个人主页
    path: '/wodePersonal',
    name: 'wodePersonal',
    component: () => import('../views/wode_personal.vue')
  },
  {//旅行者讨论区-酒馆
    path: '/discussPub',
    name: 'discussPub',
    component: () => import('../views/discuss_pub.vue')
  },
  {//旅行者讨论区-攻略
    path: '/discuss_gongLue',
    name: 'discuss_gongLue',
    component: () => import('../views/discuss_gongLue.vue')
  },
  {//官方资讯
    path: '/officialInformation',
    name: 'officialInformation',
    component: () => import('../views/official_information.vue')
  },
  {//登录
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {//注册
    path: '/zhuCe',
    name: 'zhuCe',
    component: () => import('../views/zhuCe.vue')
  },
  {//练习页
    path: '/practice',
    name: 'practice',
    component: () => import('../views/practice.vue'),
    beforeEnter: (to, from, next) => {
      console.log(to);
      console.log(from);
      alert('路由独享守卫beforeEnter已生效');
      next();
    }
  },
  {
    path: '/',
    redirect: '/home'
    // component: () => import('../views/Home.vue')
  },
  {//页面丢失
    path: '*',
    component: () => import('../views/miss.vue')
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
